package com.mianyang.zhangsan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mianyang.zhangsan.model.entity.SecAnswer;
import com.mianyang.zhangsan.service.SecAnswerService;
import com.mianyang.zhangsan.mapper.SecAnswerMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author jiangjr
*/
@Service
public class SecAnswerServiceImpl extends ServiceImpl<SecAnswerMapper, SecAnswer>
    implements SecAnswerService{

    @Override
    public List<SecAnswer> selectAnsByProblemIds(List<Long> problemIds) {
        QueryWrapper<SecAnswer> wrapper = new QueryWrapper<>();
        if (problemIds != null && problemIds.size() > 0) {
            wrapper.in("problem_id", problemIds);
            return list(wrapper);
        }
        return null;
    }

    @Override
    public List<SecAnswer> selectAnswerByProblemId(Long problemId) {
        QueryWrapper<SecAnswer> wrapper = new QueryWrapper<>();
        if (problemId != null) {
            wrapper.eq("problem_id", problemId);
            return list(wrapper);
        }
        return null;
    }

    @Override
    public void removeByProblemId(Long problemId) {
        QueryWrapper<SecAnswer> wrapper = new QueryWrapper<>();
        if (problemId != null) {
            wrapper.eq("problem_id", problemId);
            remove(wrapper);
        }
    }

    @Override
    public Long updateAndGetRightId(List<SecAnswer> answers, Long problemId) {
        Long rightAnswerId = null;
        for (SecAnswer answer : answers) {
            if (answer.getAnswerId() != null) {
                updateById(answer);
            } else {
                answer.setProblemId(problemId);
                save(answer);
            }
            if (answer.getIsRight() == 1) {
                rightAnswerId = answer.getAnswerId();
            }
        }
        return rightAnswerId;
    }
}




